/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minhphuong.dal;

import com.minhphuong.entities.Mau;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MauDAL {

    public static List<Mau> getAllMau() {
        List<Mau> listMau = null;
        Session session = null;
        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Mau");
            listMau = (List<Mau>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return listMau;
    }
    public static boolean insertMau(Mau mau) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HelperDAL.GetSession();
            tx = session.beginTransaction();
            session.save(mau);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.flush();
            session.close();
        }
        return true;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    public static Mau getMauByID(int id) {
        Mau mau = null;
        Session session = null;

        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Mau where maM=:id").setInteger("id", id);
            mau = (Mau) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            
            session.flush();
            session.close();
        }

        return mau;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
   
}
